<?php

namespace app\admin\logic;

use app\common\model\Client;

/**
 * Class ClientLogic
 * @package app\admin\logic
 *
 * 客户管理
 * 客户登陆
 */
class ClientLogic {
	function findOne( $id ) {
		return model( 'client' )->find( $id );
	}

	/*
	 * 条件查询客户
	 */
	function query( $where, $rows = 15 ) {
		return model( 'client' )->where( $where )->order('id desc')->paginate( $rows );
	}

	function queryJqGrid( $where = '', $page = 1, $rows = 20,$q='' ) {
		$client = model( 'client' );
		//$total=$client->where($where)->count();
		//$page=$page;
		$param=json_decode($q,true);
		$conditions=[];
		if(!empty($param['name'])){
			$name=$param['name'];
			$conditions['name']=array('like',"%$name%");
			//$client->where('name','like',"%$name%");
		}
		if(!empty($param['address'])){
			$address=$param['address'];
			$conditions['address']=array('like',"%$address%");
			//$client->where('address','like',"%$address%");
		}
		if(!empty($param['phone'])){
			$phone=$param['phone'];
			$conditions['phone']=array('like',"%$phone%");
			//$client->where('phone','like',"%$phone%");
		}
		$pageInfo = $client->where( $where )->where($conditions)->paginate( $rows );
		//p($pageInfo);
		$data['total'] = $pageInfo->lastPage();
		$data['page']  = $pageInfo->currentPage();
		$data['rows']  = $pageInfo->items();

		//$data['records']=$pageInfo['per_page'];
		return $data;
	}

	public function queryPaginate( $page=1,$rows=10 ) {
		$client=model("client");
		$pageInfo=$client->paginate($rows);
		return $pageInfo;
	}

	/*
	 * 新增客户
	 */
	function add( $data ) {
		$client = model( 'client' );
		if ( $data['id'] > 0 ) {
			return $client->data( $data )->allowField( true )->isUpdate( true )->save();
		} else {
			$data['id'] = null;
			$client->data( $data )->allowField( true )->save();

			return $client->id;
		}
	}

	function del( $id = 0 ) {
		return model( 'client' )->destroy( $id );
	}


}
